package com.duolaibei.business.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.duolaibei.business.R;
import com.duolaibei.business.api.ApiName;
import com.duolaibei.business.base.BaseActivity;
import com.duolaibei.business.config.Constant;
import com.duolaibei.business.config.Preferences;
import com.duolaibei.business.http.HttpUtils;
import com.duolaibei.business.model.ResponseResult;
import com.duolaibei.business.model.UserInfoData;
import com.duolaibei.business.utils.CommonUtil;
import com.duolaibei.business.utils.GsonUtil;
import com.duolaibei.business.utils.PreferenceUtil;
import com.duolaibei.business.utils.inject.Inject;
import com.duolaibei.business.view.App;
import com.duolaibei.business.view.MainActivity;

/**
 * Created by xlm on 2017/10/27 0027
 */

public class LoginActivity extends BaseActivity {

    @Inject(R.id.login_device)
    LinearLayout mLayoutDevice;
    @Inject(R.id.login_title_device)
    TextView mTextViewTitle;
    @Inject(R.id.login_change_device)
    TextView mTextViewChange;
    @Inject(R.id.device_divider)
    View mViewDivider;
    @Inject(R.id.title)
    TextView mTitle;
    @Inject(R.id.forget_btn)
    RelativeLayout mLayoutForget;
    @Inject(R.id.login_btn)
    TextView mTextViewLogin;
    private EditText mEditTextHint;
    private EditText mEditMobile;
    private EditText mEditPassword;

    private int loginType;
    private static boolean isDevice = true;//true 终端id，false 门店id
    private String loginMobile;
    private String loginPassword;

    private static final int BOSS_CODE = 1;//商户
    private static final int WORKER_CODE = 2;//员工

    @Override
    protected void onGetIntent(Intent intent) {
        super.onGetIntent(intent);
        if (intent != null) {
            loginType = intent.getIntExtra(Constant.LOGIN_MODE, -1);
            loginMobile = intent.getStringExtra(Constant.LOGIN_MOBILE);
            loginPassword = intent.getStringExtra(Constant.LOGIN_PASSWORD);
        }
    }

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_login);
    }

    @Override
    protected void initViews() {
        mEditTextHint = (EditText) findViewById(R.id.login_edit_id);
        mEditMobile = (EditText) findViewById(R.id.login_edit_mobile);
        mEditPassword = (EditText) findViewById(R.id.login_edit_password);
        if (loginType == Constant.MODE_BOSS) {
            mLayoutDevice.setVisibility(View.GONE);
            mViewDivider.setVisibility(View.GONE);
            mTitle.setText("商户登录");
        } else if (loginType == Constant.MODE_WORKER) {
            mLayoutDevice.setVisibility(View.VISIBLE);
            mViewDivider.setVisibility(View.VISIBLE);
            mTitle.setText("员工登录");
            mTextViewTitle.setText("终端ID");
            mEditTextHint.setHint("请输入终端ID");
            mTextViewChange.setText("切换门店ID");
        }
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        //注册成功 跳转商户登录
        if (!CommonUtil.isEmpty(loginMobile) && !CommonUtil.isEmpty(loginPassword)) {
            mEditMobile.setText(loginMobile);
            mEditPassword.setText(loginPassword);
        }
    }

    @Override
    protected void onSetListener() {
        mTextViewChange.setOnClickListener(this);
        mLayoutForget.setOnClickListener(this);
        mTextViewLogin.setOnClickListener(this);
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (R.id.login_change_device == v.getId()) {
            mEditTextHint.setText(null);
            if (isDevice) {
                isDevice = false;
                mTextViewTitle.setText("门店ID");
                mEditTextHint.setHint("请输入门店ID");
                mTextViewChange.setText("切换终端ID");
            } else {
                isDevice = true;
                mTextViewTitle.setText("终端ID");
                mEditTextHint.setHint("请输入终端ID");
                mTextViewChange.setText("切换门店ID");
            }
        } else if (R.id.forget_btn == v.getId()) {
            Intent intent = new Intent(getThis(), RegistAndForgetActivity.class);
            if (loginType == Constant.MODE_BOSS) {
                intent.putExtra(Constant.LOGIN_MODE, Constant.MODE_FORGET);
                startActivityForResult(intent, BOSS_CODE);
            } else if (loginType == Constant.MODE_WORKER) {
                intent.putExtra(Constant.LOGIN_MODE, Constant.MODE_FORGET);
                startActivityForResult(intent, WORKER_CODE);
            }
        } else if (R.id.login_btn == v.getId()) {
            getLoginData();
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case BOSS_CODE:
                    //商户
                case WORKER_CODE:
                    //员工
                    mEditMobile.setText(data.getStringExtra(Constant.LOGIN_MOBILE));
                    mEditPassword.setText(data.getStringExtra(Constant.LOGIN_PASSWORD));
                    break;
            }
        }
    }

    /**
     * 获取登录信息
     */
    private void getLoginData() {
        final String mobile = mEditMobile.getText().toString();
        final String password = mEditPassword.getText().toString();
        String id = mEditTextHint.getText().toString();
        if (CommonUtil.isEmpty(mobile)) {
            showToast("请输入手机号或账号");
            return;
        }
        if (CommonUtil.isEmpty(password)) {
            showToast("请输入密码");
            return;
        }
        if (loginType == Constant.MODE_WORKER) {
            if (CommonUtil.isEmpty(id)) {
                if (isDevice) {
                    showToast("请输入终端ID");
                } else {
                    showToast("请输入门店ID");
                }
                return;
            }
        }
        //员工账号：13018383737，密码：demo1，设备ID：810304，门店ID：274
        //商户账号 13812341234，密码：ab123123
        String userType = null;
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", ApiName.LOGIN);
        param.add("username", mobile);
        param.add("password", password);
        if (loginType == Constant.MODE_WORKER) {
            if (isDevice) {
                userType = "terminal";
                param.add("login_id", id);
            } else {
                userType = "shop";
                param.add("login_id", id);
            }
        } else if (loginType == Constant.MODE_BOSS) {
            userType = "merchant";
        }
        param.add("login_type", userType);
        param.add("sign", param.getMd5String());
        HttpUtils.getInstance().url(ApiName.API).params(param).
                executePost(new HttpUtils.HttpListener() {
                    @Override
                    public void onException(Throwable e) {
                        super.onException(e);
                        if (CommonUtil.isForeground(getThis())) {
                            showToast(getString(R.string.net_state));
                        }
                    }

                    @Override
                    public void onHttpSuccess(String string) {
                        super.onHttpSuccess(string);
                        if (HttpUtils.isJson(string)) {
                            ResponseResult result = GsonUtil.GsonToBean(string, ResponseResult.class);
                            if (result.getStatus() == 1) {
                                UserInfoData userInfoData = GsonUtil.GsonToBean(string, UserInfoData.class);
                                Intent intent = new Intent(getThis(), MainActivity.class);
                                intent.putExtra(Constant.UID, String.valueOf(userInfoData.getData().getId()));
                                intent.putExtra(Constant.LOGIN_ID, String.valueOf(userInfoData.getData().getLogin_id()));
                                intent.putExtra(Constant.LOGIN_TYPE, String.valueOf(userInfoData.getData().getLogin_type()));
                                if (loginType == Constant.MODE_WORKER) {
                                    //保存loginid
                                    PreferenceUtil.setParam(Preferences.LOGINID,
                                            String.valueOf(userInfoData.getData().getLogin_id()));
                                }
                                App.getApp().setUserInfo(userInfoData);//保存账号信息
                                //保存用户信息
                                PreferenceUtil.setParam(Preferences.USERNAME, mobile);
                                PreferenceUtil.setParam(Preferences.PASSWORD, password);
                                PreferenceUtil.setParam(Preferences.LOGINTYPE,
                                        String.valueOf(userInfoData.getData().getLogin_type()));
                                PreferenceUtil.setParam(Preferences.UID,
                                        String.valueOf(userInfoData.getData().getId()));
                                startActivity(intent);
                                finish();
                            } else {
                                showToast(result.getInfo());
                            }
                        } else {
                            showToast(string);
                        }
                    }
                });
    }

}
